<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1 class="login-title">
          登录 <span class="register-link-text" @click="goToRegister">/ 注册</span>
        </h1>
      </div>

      <form @submit.prevent="handleLogin">
        <div class="form-item">
          <var-input v-model="phone" placeholder="用户名" class="custom-input" />
        </div>

        <div class="form-item">
          <var-input
            v-model="password"
            :type="passwordVisible ? 'text' : 'password'"
            placeholder="密码"
            class="custom-input"
          />
        </div>

        <var-button
          class="login-button"
          type="primary"
          block
          :loading="loading"
          native-type="submit"
        >
          登录
        </var-button>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { Input as VarInput, Button as VarButton, Snackbar } from '@varlet/ui'
import '@varlet/ui/es/style'
import { login } from '@/api'

const router = useRouter()
const phone = ref('')
const password = ref('')
const passwordVisible = ref(false)
const loading = ref(false)

const goToRegister = () => {
  router.push('/register')
}

const handleLogin = async () => {
  if (!phone.value || !password.value) {
    Snackbar.warning('请输入用户名和密码')
    return
  }

  loading.value = true

  try {
    // 模拟登录请求
    await login({
      phone: phone.value,
      password: password.value,
    })

    // 这里应该是实际的登录逻辑
    Snackbar.success('登录成功')
    router.push('/home')
  } catch (error) {
    Snackbar.error('登录失败，请重试')
    console.error(error)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 360px;
  background-color: white;
  border-radius: 16px;
  padding: 30px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
}

.login-header {
  margin-bottom: 40px;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #333;
}

.register-link-text {
  font-size: 24px;
  color: #999;
  font-weight: normal;
  cursor: pointer;
}

.form-item {
  margin-bottom: 20px;
}

.custom-input {
  --input-blur-color: transparent;
  --input-focus-color: transparent;
  --input-error-color: #f44336;
  --input-focus-background: #f5f5f5;
  --input-blur-background: #f5f5f5;
  border-radius: 50px;
  height: 50px;
  padding: 0 20px;
}

.login-button {
  height: 50px;
  border-radius: 50px;
  font-size: 16px;
  margin-top: 30px;
  background-image: linear-gradient(to right, #36d1dc, #5b86e5);
  box-shadow: 0 4px 15px rgba(91, 134, 229, 0.3);
}

:deep(.var-input__controller) {
  padding: 0;
}

:deep(.var-input__icon) {
  margin-right: 12px;
}

:deep(.var-button--normal) {
  padding: 0;
}
</style>
